package com.sx.entity.design;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.sx.basic.BasicEntity;
import com.sx.basic.serializer.LongToStringSerializer;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author AdinZ
 * @date 2020/11/12 17:06
 * 组件参数
 */
@Data
@NoArgsConstructor
@TableName(value = "sx_design_params")
@Entity
@Table(name = "sx_design_params")
public class  DesignParamsEntity extends BasicEntity {

    @Id
    @GeneratedValue
    private Long id;
    @ApiModelProperty("上级参数id")
    @JSONField(serializeUsing = LongToStringSerializer.class)
    private long pid;
    @ApiModelProperty("组件id")
    @JSONField(serializeUsing = LongToStringSerializer.class)
    private long moduleId;
    @ApiModelProperty("排序")
    private int sort;
    @ApiModelProperty("变量")
    private String variable; //变量名称
    @ApiModelProperty("数值")
    private String value; //参数
    @ApiModelProperty("数据类型")
    @Enumerated(EnumType.STRING)
    private DataType type;//类型
    @ApiModelProperty("名称")
    @Column(name = "_text") @TableField("_text")
    private String text; //介绍
    @TableField(exist = false)@Transient
    private List<DesignParamsEntity> child;

    public static List<DesignParamsEntity> toTree(List<DesignParamsEntity> list,long pid) {
        if(list==null||list.size()==0)return new ArrayList<>();
        List<DesignParamsEntity> res = new ArrayList<>();
        for (DesignParamsEntity e : list) {
            if (e.getPid() == pid) {
                List<DesignParamsEntity> l = toTree(list, e.getId());
                e.setChild(l);
                res.add(e);
            }
            if (e.getChild() != null && e.getChild().size() == 0)
                e.setChild(null);
        }
        return res;
    }

}
